package com.example.administrator.familfinalcialmanagement.util;

import com.example.administrator.familfinalcialmanagement.Constants;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import static com.example.administrator.familfinalcialmanagement.Constants.URL_CREATE_ORDER;
import static com.example.administrator.familfinalcialmanagement.Constants.URL_GET_GOODS;
import static com.example.administrator.familfinalcialmanagement.Constants.URL_LOGIN;
import static com.example.administrator.familfinalcialmanagement.Constants.URL_QUERY_ORDERS;
import static com.example.administrator.familfinalcialmanagement.Constants.URL_REGISTER;
import static com.example.administrator.familfinalcialmanagement.Constants.URL_REQUEST_STORE;

public class ApiHTTP {

    public static ApiHTTP instance;

    public static ApiHTTP getInstance(){
        if (instance == null) {
            instance = new ApiHTTP();
        }
        return instance;
    }

    /**
     * 注册
     * @param name          账户
     * @param phone         电话
     * @param pwd           密码
     * @param email         邮箱
     * @param callback      联网回调
     */
    public void register(String name , String phone , String pwd, String email, StringCallback callback){
        OkGo.post(URL_REGISTER)
                .tag(this)
                .connTimeOut(30000)
                .params("account",name)
                .params("pwd",pwd)
                .params("tel",phone)
                .params("email",email)
                .execute(callback);
    }

    /**
     * 登录
     * @param name          账号
     * @param pwd           密码
     * @param callback      联网回调
     */
    public void login(String name ,String pwd,StringCallback callback){
        OkGo.post(URL_LOGIN)
                .tag(this)
                .connTimeOut(30000)
                .params("account",name)
                .params("pwd",pwd)
                .execute(callback);
    }


    /**
     * 获取商品分类
     * @param callback
     */
    public void getGoods(StringCallback callback){
        OkGo.post(URL_GET_GOODS)
                .tag(this)
                .connTimeOut(30000)
                .execute(callback);
    }


    /**
     * 查询商品详情
     * @param pageIndex
     * @param like
     * @param type_id
     * @param callback
     */
    public void requestStore(String pageIndex,String like,String type_id,StringCallback callback){
        OkGo.post(URL_REQUEST_STORE)
                .tag(this)
                .connTimeOut(30000)
                .params("pageIndex",pageIndex)
                .params("like",like)
                .params("type_id",type_id)
                .execute(callback);
    }


    /**
     * 创建订单
     * @param address               地址
     * @param is_pay                0 未支付   1支付
     * @param total_price           总价
     * @param user_id               用户Id
     * @param total_count           购买总数
     * @param goods                 商品ID的Json串
     * @param callback              联网回调
     */
    public void createOrder(String address,String is_pay,String total_price,
                            String user_id,String total_count,String goods,
                            StringCallback callback){

        LogUtil.e("address-->"+ address);
        LogUtil.e("is_pay-->"+ is_pay);
        LogUtil.e("total_price-->"+ total_price);
        LogUtil.e("user_id-->"+ user_id);
        LogUtil.e("total_count-->"+ total_count);
        LogUtil.e( "goods-->"+ goods );

        OkGo.post(URL_CREATE_ORDER)
                .tag(this)
                .connTimeOut(30000)
                .params("address",address)
                .params("is_pay",is_pay)
                .params("total_price",total_price)
                .params("user_id",user_id)
                .params("total_count",total_count)
                .params("goods",goods)
                .execute(callback);
    }


    /**
     * 查询订单
     * @param uid               个人ID
     * @param status            0 未发货  1 已发货 2 已完成
     * @param is_pay            1 未支付  2 已支付
     * @param pageIndex         页码
     * @param callback          联网回调
     */
    public void queryOrders(String uid,String status,String is_pay,String pageIndex,StringCallback callback){
        OkGo.post(URL_QUERY_ORDERS)
                .tag(this)
                .connTimeOut(30000)
                .params("uid",uid)
                .params("status",status)
                .params("is_pay",is_pay)
                .params("pageIndex",pageIndex)
                .execute(callback);
    }


}
